/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ngr.KiKi.appreciator.view;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import ngr.KiKi.appreciator.data.Student;

/**
 *
 * @author KiKi
 */
public class JPanelTabbedView extends javax.swing.JPanel
{

	private ArrayList<Student> list;
	private DefaultTableModel model;

	/**
	 * Creates new form MainPanel
	 */
	public JPanelTabbedView ()
	{
		initComponents ();

		init ();
	}

	private void init ()
	{
		list = new ArrayList<> ();

		model = (DefaultTableModel) jTableMain.getModel ();
		jTableMain.setModel (model);
	}

	public void load (ArrayList<Student> list)
	{
		clear ();

		list.stream ().forEach ((student) ->
		{
			model.addRow (new Object[]
			{
				student.getfullname (), student.getNotes ()[0], student.getMean (), student.getAppreciation ()
			});
		});
		resizeColumnWidth ();
	}

	private void clear ()
	{
		int count = model.getRowCount ();
		for (int i = 0; i < count; i++, model.removeRow (0));
	}

	private void resizeColumnWidth ()
	{
		final TableColumnModel columnModel = jTableMain.getColumnModel ();
		for (int column = 0; column < jTableMain.getColumnCount (); column++)
		{
			int width = 50; // Min width
			for (int row = 0; row < jTableMain.getRowCount (); row++)
			{
				TableCellRenderer renderer = jTableMain.getCellRenderer (row, column);
				Component comp = jTableMain.prepareRenderer (renderer, row, column);
				width = Math.max (comp.getPreferredSize ().width, width);
			}
			columnModel.getColumn (column).setPreferredWidth (width);
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableMain = new javax.swing.JTable();

        setOpaque(false);

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTableMain.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String []
            {
                "Elèves", "T1", "A", "Appréciations"
            }
        )
        {
            Class[] types = new Class []
            {
                java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex)
            {
                return types [columnIndex];
            }
        });
        jTableMain.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTableMain.setFillsViewportHeight(true);
        jTableMain.setOpaque(false);
        jTableMain.setPreferredSize(new java.awt.Dimension(460, 64));
        jTableMain.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableMain);
        if (jTableMain.getColumnModel().getColumnCount() > 0)
        {
            jTableMain.getColumnModel().getColumn(0).setPreferredWidth(150);
            jTableMain.getColumnModel().getColumn(1).setMinWidth(50);
            jTableMain.getColumnModel().getColumn(1).setMaxWidth(50);
            jTableMain.getColumnModel().getColumn(2).setMinWidth(50);
            jTableMain.getColumnModel().getColumn(2).setMaxWidth(50);
            jTableMain.getColumnModel().getColumn(3).setMinWidth(250);
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 863, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableMain;
    // End of variables declaration//GEN-END:variables
}
